<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API前端</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .container {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }
        .button-group {
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
        }
        button {
            padding: 10px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        #result {
            border: 1px solid #ddd;
            padding: 15px;
            border-radius: 4px;
            background-color: #f9f9f9;
            min-height: 100px;
            white-space: pre-wrap;
        }
        .input-group {
            margin-bottom: 10px;
        }
        label {
            display: inline-block;
            width: 120px;
        }
        input {
            padding: 8px;
            width: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>API前端</h1>

        <div class="button-group">
            <div>
                <h2>消息API</h2>
                <div class="input-group">
                    <label for="messagePage">页码:</label>
                    <input type="number" id="messagePage" value="1" min="1">
                </div>
                <div class="input-group">
                    <label for="messagePerPage">每页数量:</label>
                    <input type="number" id="messagePerPage" value="10" min="1">
                </div>
                <button onclick="getMessages()">获取消息列表</button>

                <div class="input-group">
                    <label for="messageId">消息ID:</label>
                    <input type="number" id="messageId" value="1" min="1">
                </div>
                <button onclick="getMessageById()">获取单条消息</button>
            </div>

            <div>
                <h2>地区API</h2>
                <div class="input-group">
                    <label for="regionPage">页码:</label>
                    <input type="number" id="regionPage" value="1" min="1">
                </div>
                <div class="input-group">
                    <label for="regionPerPage">每页数量:</label>
                    <input type="number" id="regionPerPage" value="10" min="1">
                </div>
                <button onclick="getRegions()">获取地区列表</button>

                <div class="input-group">
                    <label for="regionId">地区ID:</label>
                    <input type="number" id="regionId" value="1" min="1">
                </div>
                <button onclick="getRegionById()">获取单个地区</button>
            </div>
        </div>

        <h2>API返回结果:</h2>
        <div id="result">点击上面的按钮测试API...</div>
    </div>

    <script>
        const apiBaseUrl = 'http://127.0.0.1:9999/api'; // 根据你的实际API地址修改

        function displayResult(data) {
            document.getElementById('result').textContent = JSON.stringify(data, null, 2);
        }

        function handleError(error) {
            console.error('Error:', error);
            document.getElementById('result').textContent = '请求出错: ' + error.message;
        }

        // 获取消息列表
        function getMessages() {
            const page = document.getElementById('messagePage').value;
            const perPage = document.getElementById('messagePerPage').value;
            const url = `${apiBaseUrl}/messages?page=${page}&per_page=${perPage}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取单条消息
        function getMessageById() {
            const messageId = document.getElementById('messageId').value;
            const url = `${apiBaseUrl}/messages/${messageId}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取地区列表
        function getRegions() {
            const page = document.getElementById('regionPage').value;
            const perPage = document.getElementById('regionPerPage').value;
            const url = `${apiBaseUrl}/regions?page=${page}&per_page=${perPage}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }

        // 获取单个地区
        function getRegionById() {
            const regionId = document.getElementById('regionId').value;
            const url = `${apiBaseUrl}/regions/${regionId}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => displayResult(data))
                .catch(error => handleError(error));
        }
    </script>
</body>
</html>